package com.rayo.accesscontrollercopy.model.db.dao;

import com.litesuits.orm.db.assit.QueryBuilder;
import com.rayo.accesscontrollercopy.model.db.DbHelp;
import com.rayo.accesscontrollercopy.model.db.bean.CardUserData;

import java.util.Date;
import java.util.List;

/**
 * Created by yanghaojie on 16/7/27.
 */
public class CardUserImpl implements CardUserDao {
    @Override
    public List<CardUserData> findAll() {
        return DbHelp.getQueryAll(CardUserData.class);
    }

    @Override
    public CardUserData findByUserId(int userid) {
        List<CardUserData> list = DbHelp.getQueryByWhere(CardUserData.class, CardUserData.USER_ID,
                new String[]{String.valueOf(userid)});
        if (list.size() > 0)
            return list.get(0);
        return new CardUserData();
    }

    @Override
    public void addAll(List<CardUserData> cardUser) {
        DbHelp.insertAll(cardUser);
    }

    @Override
    public Date addCardUser(CardUserData data) {

        QueryBuilder qb = new QueryBuilder(CardUserData.class).whereEquals(CardUserData.USER_ID, data.getUserid());
        List<CardUserData> list = DbHelp.liteOrm.query(qb);

        if (null == list || list.size() == 0) {
            data.setEditime(new Date());
            DbHelp.insert(data);
            return data.getEditime();
        } else {
            CardUserData temp = list.get(0);
            temp.setUserid(data.getUserid());
            temp.setGroupid(data.getGroupid());
            temp.setBegin(data.getBegin());
            temp.setEnd(data.getEnd());
            temp.setPlan(data.getPlan());
            temp.setEditime(new Date());
            DbHelp.update(temp);
            return temp.getEditime();
        }
    }

    @Override
    public void updateAll(List<CardUserData> cardUser) {
        DbHelp.updateALL(cardUser);
    }

    @Override
    public void updateCardUser(CardUserData cardUser) {
        DbHelp.update(cardUser);
    }

    @Override
    public void deleteAll() {
        DbHelp.deleteAll(CardUserData.class);
    }

    @Override
    public void deleteByUserId(int userid) {
        DbHelp.deleteWhere(CardUserData.class, CardUserData.USER_ID,
                new String[]{String.valueOf(userid)});

    }
}
